package com.yhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhx.iservice.IRoleService;
import com.yhx.mapper.RoleMapper;
import com.yhx.mapper.RolePermissionMapper;
import com.yhx.pojo.Role;
import com.yhx.pojo.RolePermission;
import com.yhx.utils.MessageConstant;
import com.yhx.utils.PageResult;
import com.yhx.utils.QueryPageBean;
import com.yhx.utils.Result;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class RoleServiceImpl implements IRoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Override
    public PageResult listpage(QueryPageBean queryPageBean) {
        QueryWrapper<Role> queryWapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if(queryString!=null&&queryString.length()>0){
            queryWapper.like("name", queryString)
                    .or().like("keyword", queryString);
        }
        //创建一个分页对象
        Page<Role> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询
        Page<Role> permission = roleMapper.selectPage(page, queryWapper);
        //返回查询结果
        return new PageResult(permission.getTotal(), permission.getRecords());
    }

    @Override
    public Result save(Role role) {
        List<Integer> list = role.getPermissionids();
        if(list==null && list.size()==0){
            return new Result(false, "请选择权限");
        }
        int insert = roleMapper.insert(role);
        if(insert==0){
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        //获取主键值
        Integer id = role.getId();
        for (Integer integer : list) {
            RolePermission rolePermission = new RolePermission(id, integer);
            rolePermissionMapper.insert(rolePermission);
        }
        return new Result(true,MessageConstant.ADD_ROLE_SUCCESS );
    }

    @Override
    public Result getByid(int id) {
        Role role = roleMapper.selectById(id);
        if(role==null){
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
        //获取主键
        Integer id1 = role.getId();
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id1);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
        ArrayList<Integer> list = new ArrayList<>();
        for (RolePermission permission : rolePermissions) {
            list.add(permission.getPermissionId());
        }
        role.setPermissionids(list);
        return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
    }

    @Override
    public Result update(Role role) {
        List<Integer> list = role.getPermissionids();
        if(list==null||list.size()==0){
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        int i = roleMapper.updateById(role);
        if(i==0){
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
        Integer id = role.getId();
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        int delete = rolePermissionMapper.delete(queryWrapper);

        for (Integer integer : list) {
            RolePermission rolePermission = new RolePermission(id, integer);
            rolePermissionMapper.insert(rolePermission);
        }
        return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
    }

    @Override
    public Result delete(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        for (Integer integer : list) {
            QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", integer);
            rolePermissionMapper.delete(queryWrapper);
        }
        int i = roleMapper.deleteBatchIds(list);
        if (i>0){
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        }
        return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
    }

    @Override
    public Result list() {
        List<Role> roles = roleMapper.selectList(null);
        if(roles!=null){
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,roles);
        }
        return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
    }

    @Override
    public List<Role> roleListByUserId(int userId) {
        return roleMapper.roleListByUserId(userId);
    }

}
